package com.tsinghuabigdata.edu.ddmath.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tsinghuabigdata.edu.ddmath.R;


/**
 * Created by Administrator on 2016/12/23.
 */

public class CoursePager extends FrameLayout implements View.OnClickListener {

    private LinearLayout mFaultLayout;
    private Button mBtnRetry;
    private LinearLayout mEmptyLayout;
    private LinearLayout mLoadingLayout;
    private ImageView mGivLoading;
    private TextView mTvLoading;

    private View              mTargetView;
    private OnClickListener   mListener;
    private AnimationDrawable animationDrawable;

    public CoursePager(Context context) {
        this(context, null);
    }

    public CoursePager(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(context, R.layout.view_course_pager, this);
        initView();
    }

    //    public LoadingPager(Context context, AttributeSet attrs, int defStyleAttr) {
    //        super(context, attrs, defStyleAttr);
    //        initialize();
    //    }

    /**
     * 初始化界面元素
     */
    private void initView() {
        mFaultLayout = (LinearLayout) findViewById(R.id.fault_layout);
        mBtnRetry = (Button) findViewById(R.id.btn_retry);
        mEmptyLayout = (LinearLayout) findViewById(R.id.empty_layout);
        mLoadingLayout = (LinearLayout) findViewById(R.id.loading_layout);
        mGivLoading = (ImageView) findViewById(R.id.giv_loading);
        mTvLoading = (TextView) findViewById(R.id.tv_loading);
        mBtnRetry.setOnClickListener(this);

        animationDrawable = (AnimationDrawable) mGivLoading.getDrawable();
        startAnim();
    }

    private void startAnim() {
        animationDrawable.start();
    }

    private void stopAnim() {
        animationDrawable.stop();
    }

    public void showFault() {
        mLoadingLayout.setVisibility(GONE);
        mFaultLayout.setVisibility(VISIBLE);
        mEmptyLayout.setVisibility(GONE);
//        mTargetView.setVisibility(GONE);
        animationDrawable.stop();
    }

    public void showEmpty() {
        mLoadingLayout.setVisibility(GONE);
        mFaultLayout.setVisibility(GONE);
        mEmptyLayout.setVisibility(VISIBLE);
//        mTargetView.setVisibility(GONE);
        animationDrawable.stop();
    }

    public void showLoading() {
        mLoadingLayout.setVisibility(VISIBLE);
        mFaultLayout.setVisibility(GONE);
        mEmptyLayout.setVisibility(GONE);
//        mTargetView.setVisibility(GONE);
        animationDrawable.start();
    }

    public void showTarget() {
        mLoadingLayout.setVisibility(GONE);
        mFaultLayout.setVisibility(GONE);
        mEmptyLayout.setVisibility(GONE);
//        mTargetView.setVisibility(VISIBLE);
        animationDrawable.stop();
    }

    /**
     * 绑定目标控件
     *
     * @param view
     */
    public void setTargetView(View view) {
        mTargetView = view;
//        mTargetView.setVisibility(GONE);
    }

    /**
     * 绑定点击事件
     *
     * @param listener
     */
    public void setListener(OnClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
            mListener.onClick(v);
            Log.i("myview", "onClick");
        }
    }
}
